/*
 * pearl.c
 *
 *  Created on: 05/12/2009
 *      Author: Kimi
 */
#include "input/pearl.h"

unsigned int pearl_state_left;
unsigned int pearl_state_right;
unsigned int pearl_state_down;
unsigned int pearl_state_up;

void pearl_Init() {
	PEARL_INPUT_DIR &= ~(1 << PEARL_CENTER_PIN);
	PEARL_INPUT_DIR &= ~(1 << PEARL_DOWN_PIN);
	PEARL_INPUT_DIR &= ~(1 << PEARL_LEFT_PIN);
	PEARL_INPUT_DIR &= ~(1 << PEARL_RIGHT_PIN);
	PEARL_INPUT_DIR &= ~(1 << PEARL_UP_PIN);

	pearl_state_down = 0;
	pearl_state_left = 0;
	pearl_state_up = 0;
	pearl_state_right = 0;

	if (!(PEARL_INPUT_PIN & (1 << PEARL_LEFT_PIN))) {
		pearl_state_left = 1;
	}
	if (!(PEARL_INPUT_PIN & (1 << PEARL_RIGHT_PIN))) {
		pearl_state_right = 1;
	}
	if (!(PEARL_INPUT_PIN & (1 << PEARL_UP_PIN))) {
		pearl_state_up = 1;
	}
	if (!(PEARL_INPUT_PIN & (1 << PEARL_DOWN_PIN))) {
		pearl_state_down = 1;
	}

	#ifdef PEARL_BLUE
	PEARL_OUTPUT_DIR |= (1 << PEARL_BLUE);
	PEARL_OUTPUT_CLEAR = (1 << PEARL_BLUE);
	#endif
	#ifdef PEARL_GREEN
		PEARL_OUTPUT_DIR |= (1 << PEARL_GREEN);
		PEARL_OUTPUT_CLEAR = (1 << PEARL_GREEN);
	#endif
	#ifdef PEARL_RED
		PEARL_OUTPUT_DIR |= (1 << PEARL_RED);
		PEARL_OUTPUT_CLEAR = (1 << PEARL_RED);
	#endif
	#ifdef PEARL_WHITE
		PEARL_OUTPUT_DIR |= (1 << PEARL_WHITE);
		PEARL_OUTPUT_CLEAR = (1 << PEARL_WHITE);
	#endif
}

unsigned char pearl_IsMovingLeft() {
	if ((!(PEARL_INPUT_PIN & (1 << PEARL_LEFT_PIN))) && (pearl_state_left == 0)) {
		pearl_state_left = 1;
		return 1;
	}
	else if (((PEARL_INPUT_PIN & (1 << PEARL_LEFT_PIN))) && (pearl_state_left == 1)) {
		pearl_state_left = 0;
		return 1;
	}
	return 0;
}

unsigned char pearl_IsMovingRight() {
	if ((!(PEARL_INPUT_PIN & (1 << PEARL_RIGHT_PIN))) && (pearl_state_right == 0)) {
		pearl_state_right = 1;
		return 1;
	}
	else if (((PEARL_INPUT_PIN & (1 << PEARL_RIGHT_PIN))) && (pearl_state_right == 1)) {
		pearl_state_right = 0;
		return 1;
	}
	return 0;
}

unsigned char pearl_IsMovingDown() {
	if ((!(PEARL_INPUT_PIN & (1 << PEARL_DOWN_PIN))) && (pearl_state_down == 0)) {
		pearl_state_down = 1;
		return 1;
	}
	else if (((PEARL_INPUT_PIN & (1 << PEARL_DOWN_PIN))) && (pearl_state_down == 1)) {
		pearl_state_down = 0;
		return 1;
	}
	return 0;
}

unsigned char pearl_IsMovingUp() {
	if ((!(PEARL_INPUT_PIN & (1 << PEARL_UP_PIN))) && (pearl_state_up == 0)) {
		pearl_state_up = 1;
		return 1;
	}
	else if (((PEARL_INPUT_PIN & (1 << PEARL_UP_PIN))) && (pearl_state_up == 1)) {
		pearl_state_up = 0;
		return 1;
	}
	return 0;
}

unsigned char pearl_IsCenterPressed() {
	if (!(PEARL_INPUT_PIN & (1 << PEARL_CENTER_PIN))) {
		return 1;
	}
	return 0;
}

#ifdef PEARL_RED
void pearl_RedLight(unsigned char state) {
	if (state != 0) {
		PEARL_OUTPUT_SET = (1 << PEARL_RED);
	} else {
		PEARL_OUTPUT_CLEAR = (1 << PEARL_RED);
	}
}
#endif


#ifdef PEARL_GREEN
void pearl_GreenLight(unsigned char state) {
	if (state != 0) {
		PEARL_OUTPUT_SET = (1 << PEARL_GREEN);
	} else {
		PEARL_OUTPUT_CLEAR = (1 << PEARL_GREEN);
	}
}
#endif

#ifdef PEARL_BLUE
void pearl_BlueLight(unsigned char state) {
	if (state != 0) {
		PEARL_OUTPUT_SET = (1 << PEARL_BLUE);
	} else {
		PEARL_OUTPUT_CLEAR = (1 << PEARL_BLUE);
	}
}
#endif

#ifdef PEARL_WHITE
void pearl_WhiteLight(unsigned char state) {
	if (state != 0) {
		PEARL_OUTPUT_SET = (1 << PEARL_WHITE);
	} else {
		PEARL_OUTPUT_CLEAR = (1 << PEARL_WHITE);
	}
}
#endif

void pearl_TestLights() {
#ifdef PEARL_RED
	pearl_RedLight(1);
	delay_ms(500);
	pearl_RedLight(0);
#endif
#ifdef PEARL_GREEN
	pearl_GreenLight(1);
	delay_ms(500);
	pearl_GreenLight(0);
#endif
#ifdef PEARL_BLUE
	pearl_BlueLight(1);
	delay_ms(500);
	pearl_BlueLight(0);
#endif
#ifdef PEARL_WHITE
	pearl_WhiteLight(1);
	delay_ms(500);
	pearl_WhiteLight(0);
#endif
}
